package com.coc.controller.system;



import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coc.core.utils.CheckObjField;
import com.coc.core.utils.StringRelaUtils;
import com.coc.entity.core.JsonResult;
import com.coc.entity.core.ResultTool;
import com.coc.entity.system.SysRole;
import com.coc.entity.system.SysRolePermission;
import com.coc.service.system.SysRoleService;
import com.coc.service.system.SysRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户角色表(SysRole)表控制层
 *
 * @author songju
 * @since 2020-02-10 19:12:42
 */
@RestController
@RequestMapping("sysRole")
public class SysRoleController extends ApiController {
    /**
     * 服务对象
     */
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param sysRole 查询实体
     * @return 所有数据
     */
    @PostMapping("selectAll")
    public JsonResult selectAll(Page<SysRole> page, SysRole sysRole) {
        JsonResult result = ResultTool.success();
        QueryWrapper<SysRole> sqw = new CheckObjField<SysRole>().getFields(sysRole);
        result.setData(this.sysRoleService.page(page, sqw));
        return result;
    }

    @PostMapping("selectAll1")
    public JsonResult selectAll1() {
        JsonResult result = ResultTool.success();
        List<SysRole> list = sysRoleService.list();
        result.setData(list);
        return result;
    }
    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @PostMapping("/delBatchByIds")
    public JsonResult delBatchByIds(@RequestBody String ids){
        JSONObject jsonObject = JSONObject.parseObject(ids);
        String idss = jsonObject.getString("id");
        List<String> ss = StringRelaUtils.strToList(idss);
        QueryWrapper<SysRolePermission> srp = new QueryWrapper<>();
        srp.in("role_id",ss);
        //删除sys_user_role及sys_role_permission表中与该角色对应的关系
        boolean remove = sysRolePermissionService.remove(srp);
        boolean b = sysRoleService.removeByIds(ss);
        if (b&&remove){
            return ResultTool.success("删除成功");
        }else {
            return ResultTool.fail();
        }
    }
    @PostMapping("/addUser")
    public JsonResult addUser(@RequestBody String json) {

        JsonResult jsonResult = sysRoleService.addUser(json);
        return ResultTool.success();
    }
    @PostMapping("/getPermitByRole")
    public JsonResult getPermitByRole(@RequestBody String json) {
        JsonResult result = ResultTool.success();
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer role_id = jsonObject.getInteger("role_id");
        QueryWrapper<SysRolePermission> spq = new QueryWrapper<>();
        spq.eq("role_id",role_id);
        List<SysRolePermission> list = sysRolePermissionService.list(spq);
        result.setData(list);
        return result;
    }
    @PostMapping("/editRole")
    public JsonResult editRole(@RequestBody String json) {

        sysRoleService.editRole(json);
        return ResultTool.success();
    }
}